package cn.ichiva.dl.tushare.analy;

import lombok.extern.slf4j.Slf4j;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 第二阶段数据准备
 *
 * 当发出买入指令时买入
 * 如果没有筹码买入则记录一个虚拟买入
 * 当发出卖出指令的时优先减去一个虚拟买入
 * 所有虚拟买入扣除后才实际卖出
 */
@Slf4j
public class P4Data {

    public static final List<DayINDArray> LIST = new ArrayList<>();
    public static final List<DayINDArray> TRAIN_1 = new ArrayList<>();
    public static final List<DayINDArray> TRAIN_2 = new ArrayList<>();
    public static final List<DayINDArray> TEST_0 = new ArrayList<>();

    static {
        try {
            List<String> lines = Files.readAllLines(Paths.get("C:\\Users\\Chiva\\git\\gitee\\temp\\wall-street-server\\dl4j-training-camp\\src\\main\\resources\\p2data.csv"));
            int len = lines.get(0).split(",").length - 1;
            int border_1 = lines.size() / 3;
            int border_2 = lines.size() * 2 / 3;
            int size = lines.size();
            for (int i = 0; i < size; i++) {
                String[] arr = lines.get(i).split(",");
                double[] tem = new double[len];
                for (int j = 0; j < tem.length; j++) {
                    tem[j] = Double.parseDouble(arr[j + 1]);
                }

                INDArray indArr = Nd4j.create(tem).reshape(1,17,1);
                DayINDArray data = new DayINDArray();
                data.setDay(arr[0]);
                data.setData(indArr);

                LIST.add(data);
                if(i <= border_1) TRAIN_1.add(data);
                else if(i <= border_2) TRAIN_2.add(data);
                else TEST_0.add(data);
            }
            String train_1_Start = TRAIN_1.get(0).getDay();
            String train_1_End = TRAIN_1.get(TRAIN_1.size() - 1).getDay();

            String train_2_Start = TRAIN_2.get(0).getDay();
            String train_2_End = TRAIN_2.get(TRAIN_2.size() - 1).getDay();

            String testStart = TEST_0.get(0).getDay();
            String testEnd = TEST_0.get(TEST_0.size() - 1).getDay();
            log.info("数据初始化完成,train_1=[{},{}], train_2=[{},{}], test=[{},{}]",train_1_Start,train_1_End,train_2_Start,train_2_End,testStart,testEnd);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
